/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.user.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import com.pig4cloud.pigx.user.dto.UserLogsDTO;
import com.pig4cloud.pigx.user.entity.UserLogs;
import com.pig4cloud.pigx.user.mapstruct.UserLogsStruct;
import com.pig4cloud.pigx.user.service.UserLogsService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 平台用户日志
 *
 * @author pigx code generator
 * @date 2022-08-10 16:31:53
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/userlogs" )
@Api(value = "userlogs", tags = "平台用户日志管理")
public class UserLogsController {

    private final  UserLogsService userLogsService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param userLogsDTO 平台用户日志
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('user_userlogs_view')" )
    public R getUserLogsPage(Page page, UserLogsDTO userLogsDTO) {
		UserLogs userLogs = UserLogsStruct.INSTANCE.dto2userLogs(userLogsDTO);
		//时间筛选
		String time = null;
		if (userLogsDTO.getCreateDate() != null) {
			userLogs.setCreateDate(null);
			time = userLogsDTO.getCreateDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
		}
		return R.ok(userLogsService.page(page, Wrappers.query(userLogs)
				.like(time != null,"create_date",time)));
    }


    /**
     * 通过id查询平台用户日志
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('user_userlogs_view')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(userLogsService.getById(id));
    }

    /**
     * 新增平台用户日志
     * @param userLogs 平台用户日志
     * @return R
     */
    @ApiOperation(value = "新增平台用户日志", notes = "新增平台用户日志")
    @SysLog("新增平台用户日志" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('user_userlogs_add')" )
    public R save(@RequestBody UserLogs userLogs) {
        return R.ok(userLogsService.save(userLogs));
    }

    /**
     * 修改平台用户日志
     * @param userLogs 平台用户日志
     * @return R
     */
    @ApiOperation(value = "修改平台用户日志", notes = "修改平台用户日志")
    @SysLog("修改平台用户日志" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('user_userlogs_edit')" )
    public R updateById(@RequestBody UserLogs userLogs) {
        return R.ok(userLogsService.updateById(userLogs));
    }

    /**
     * 通过id删除平台用户日志
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除平台用户日志", notes = "通过id删除平台用户日志")
    @SysLog("通过id删除平台用户日志" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('user_userlogs_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(userLogsService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param userLogs 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('user_userlogs_export')" )
    public List<UserLogs> export(UserLogs userLogs) {
        return userLogsService.list(Wrappers.query(userLogs));
    }

	/**
	 * 插入日志
	 * @param userLogs 日志实体
	 * @return true/false
	 */
	@Inner
	@PostMapping("/saveLog")
	public R<Boolean> saveLog(@Valid @RequestBody UserLogs userLogs) {
		return R.ok(userLogsService.save(userLogs));
	}
}
